package com.example.hy2110szyx.controller;

import com.example.hy2110szyx.common.DatatablesJSON;
import com.example.hy2110szyx.common.MyJsonResult;
import com.example.hy2110szyx.entity.Distribution;
import com.example.hy2110szyx.entity.FlightInfo;
import com.example.hy2110szyx.entity.Route;
import com.example.hy2110szyx.service.FlightService;
import com.example.hy2110szyx.vo.PageVo;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/flight", produces = "application/json")
@Slf4j
@CrossOrigin(origins = "*")
public class FlightController {

    private FlightService flightService;


    @Autowired
    public FlightController(FlightService flightService) {
        this.flightService = flightService;
    }

    @GetMapping("/queryId/{id}")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult queryFlightAndRoute(@PathVariable("id") Integer id) {
        List<FlightInfo> flightInfos = flightService.queryById(id);
        return new MyJsonResult(0, null, flightInfos, null);
    }

    @GetMapping("/query/{fno}")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult queryOneFlight(@PathVariable("fno") String fno) {
        FlightInfo flightInfos = flightService.queryByFno(fno);
        return new MyJsonResult(0, null, null, flightInfos);
    }

    @GetMapping("/page")
    @ResponseBody
    public DatatablesJSON<FlightInfo> query(
            int draw,
            @RequestParam("order[0][column]") int orderColumn,
            @RequestParam("order[0][dir]") String orderDir,
            int start, int length, String fno, Integer type, String departureTime
    ) throws ParseException {
        log.debug("departureTime is" + departureTime);
        String[] columns = {null, "ffno", "type", "departureTime", "landingTime","fano", "balance", null};
        FlightInfo flightConn = null;
        if ("".equals(departureTime)) {
            flightConn = new FlightInfo(fno, type);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date newDepartureTime = sdf.parse(departureTime);
            flightConn = new FlightInfo(fno, type, newDepartureTime);
        }
        log.debug("order[0][column]=" + orderColumn);
        log.debug("order[0][dir]=" + orderDir);
        log.debug("flightConn is {}", flightConn);
        PageVo<FlightInfo> pageVo = new PageVo(start, length, columns[orderColumn], orderDir, flightConn);
        Page<FlightInfo> page = flightService.queryAllFlightsForPage(pageVo);
        DatatablesJSON<FlightInfo> datatablesJSON = new DatatablesJSON<>();
        datatablesJSON.setDraw(draw);
        datatablesJSON.setRecordsFiltered(page.getTotal());
        datatablesJSON.setData(page.getResult());
        return datatablesJSON;
    }

    @GetMapping("/query")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult queryAllFlights() {
        List<FlightInfo> flightInfos = flightService.queryAll();
        return new MyJsonResult(0, null, flightInfos, null);
    }

    /**
     * 添加航班
     *
     * @param flight
     * @return
     */
    @PostMapping("/addFlight")
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public MyJsonResult addOneFlight(@RequestBody FlightInfo flight) {
        FlightInfo existsFlight = null;
        String fno = null;//航班编号
        do {
            //防止航班编号重复
            int ffno = (int)((Math.random()*9+1)*1000000);
            log.debug("7位随机数:"+ffno);
            fno = String.valueOf(ffno);
            existsFlight = flightService.queryByFno(fno);
        } while (null != existsFlight);
        flight.setFno(fno);
        flightService.insertOneFlight(flight);
        return new MyJsonResult(0, "添加成功", null, fno);
    }

    @PostMapping("/flightInfo")
    @ResponseStatus(HttpStatus.CREATED)
    public MyJsonResult insertOneFlight(@RequestBody FlightInfo flightInfo) {
        flightService.insertOneFlight(flightInfo);
        return new MyJsonResult(0, null, null, flightInfo);
    }



    @PutMapping("/modFlight/{id}")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult modOneFlight(@PathVariable Integer id, @RequestBody FlightInfo flightInfo) {
        flightInfo.setId(id);
        flightService.updateOneFlight(flightInfo);
        return new MyJsonResult(0, null, null, flightInfo);
    }


    @DeleteMapping("/delSomeFlight")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult delSomeFlight(@RequestBody Integer[] ids) {
        flightService.deleteSomeById(ids);
        return new MyJsonResult(0, "删除成功", null, null);
    }




}
